﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.FrameWork;
using System.Collections.Specialized;
using System.Web.UI.WebControls;

namespace Eis51.CampanyCMS.Business.BaseInfoMng
{
    /// <summary>
    /// Tag 的摘要说明。
    /// </summary>
    public class Tag
    {

        const string TagEnumStr = "TagEnum";





        /// <summary>
        /// 多语言Enum数据
        /// </summary>
        private HybridDictionary TagEnumData
        {
            get
            {
                if (Dict.CacheData[CacheName.LanguageDictTag] == null)
                {
                    HybridDictionary hd = new HybridDictionary();
                    DataTable dt = BaseDictTagBLL.GetInstance().GetDataTable("select * from Base_Dict_Tag");
                    hd.Add(TagEnumStr, dt);
                    Dict.CacheData[CacheName.LanguageDictTag] = hd;
                }
                return Dict.CacheData[CacheName.LanguageDictTag] as HybridDictionary;
            }
        }
        /// <summary>
        /// 获取多语言化标签
        /// </summary>
        /// <remarks> 
        /// 实现流程： 
        ///		1.使用TagEnumDictView查询  
        ///		2.返回标签   
        ///		3.结束    
        /// </remarks> 
        /// <exception cref="Exception">
        /// 异常描述：出现数据库异常,抛出并回滚事务  
        /// </exception>
        /// <param name="StateEnum">枚举</param>
        /// <returns>标签字符串</returns>
        public string this[Enum StateEnum]
        {
            get
            {
                //                int StateNo =(int)Enum.Parse(StateEnum.GetType(),StateEnum.ToString());
                string StateNo = ((int)Enum.Parse(StateEnum.GetType(), StateEnum.ToString())).ToString();
                return this[StateEnum.GetType().Name, StateNo, AppContext.LCID];
            }
        }

        /// <summary>
        /// 获取多语言化标签
        /// </summary>
        /// <remarks> 
        /// 实现流程： 
        ///		1.使用TagEnumDictView查询  
        ///		2.返回标签   
        ///		3.结束    
        /// </remarks> 
        /// <exception cref="Exception">
        /// 异常描述：出现数据库异常,抛出并回滚事务  
        /// </exception>
        /// <returns>标签字符串</returns>
        public string this[string EnumType, string EnumValue, int LanguageType]
        {
            get
            {
                string tag = string.Empty;
                int LCID = AppContext.LCID;
                DataRowView[] drv = TagEnumDictView("LCID=" + LCID, "ENUM_TYPE_NAME,ENUM_VALUE")
                    .FindRows(new string[] { EnumType, EnumValue });
                if (drv.Length > 0)
                {
                    tag = drv[0]["TAG_NAME"].ToString();
                }
                return tag;
            }
        }

        /// <summary>
        /// 类型排序视图
        /// </summary>
        private DataView ViewByType
        {
            get
            {
                return TagEnumDictView("LCID=" + AppContext.LCID, "ENUM_TYPE_NAME");
            }
        }

        /// <summary>
        /// 根据类别绑定数据控件
        /// </summary>
        /// <remarks> 
        /// 实现流程： 
        ///		1.使用GetTypeView查询  
        ///		2.返回标签集会   
        ///		3.绑定     
        /// </remarks> 
        /// <param name="listContrl">列表控件</param>
        /// <param name="enumType">枚举类型</param>
        public void BindListControl(ListControl listContrl, Enum enumType)
        {
            BindListControl(listContrl, enumType.ToString());
        }

        /// <summary>
        /// 根据类别绑定数据控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="enumTypeName"></param>
        public void BindListControl(ListControl control, string enumTypeName)
        {
            control.DataSource = ViewByType.FindRows(enumTypeName);
            control.DataTextField = "TAG_NAME";
            control.DataValueField = "ENUM_VALUE";
            control.DataBind();
        }

        /// <summary>
        /// 多语言数据
        /// </summary>
        /// <param name="filter">过滤字串</param>
        /// <param name="sort">排序</param>
        /// <returns>视图</returns>
        private DataView TagEnumDictView(string filter, string sort)
        {
            HybridDictionary hd = TagEnumData;
            int LCID = AppContext.LCID;
            string key = filter + sort + LCID;
            if (hd[key] == null)
            {
                hd[key] = new DataView(hd[TagEnumStr] as DataTable, filter, sort, DataViewRowState.CurrentRows);
            }
            return hd[key] as DataView;
        }


    }
}
